﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using AlanUtilities;
using System.Text;
using DBAccessor.Accessor;
using DBList;
using DBList.Entities;
using System.Data.SqlClient;

public partial class AddBarCode : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		int nActID = 0;
		int.TryParse(Request.QueryString["action"], out nActID);
		if (nActID != 1 && nActID != 2)
			return;
		if(nActID == 1){
		    HttpCookie hc = Request.Cookies.Get("uploadmode");
		    string str = hc.Value;
			if (str != "11")
				return;
			String strResult = "";
			byte[] input = Request.BinaryRead(3);
			if (input.Length == 3 && Request.TotalBytes == (input[1] + input[2] + 3)) {
				int nNameLen = input[1];
				int nPWDLen = input[2];
				input = Request.BinaryRead(input[1] + input[2]);

				byte[] pName = new Byte[nNameLen];
				byte[] pPWD = new Byte[nPWDLen];
				Array.Copy(input, 0, pName, 0, nNameLen);
				Array.Copy(input, nNameLen, pPWD, 0, nPWDLen);
				String strName = Encoding.UTF8.GetString(pName);
				String strPWD = Encoding.UTF8.GetString(pPWD);
				strName = OperateControl.StringDecrypt(strName);
				strPWD = OperateControl.StringDecrypt(strPWD);

				MemberAccessor memberSystem = new MemberAccessor();
				strPWD = OperateControl.GetMD5String(strPWD);
				UInt64 nRet = memberSystem.Validate(strName, strPWD);
				uint uID = (uint)(nRet >> 32), uGroupID = (uint)nRet;
				if (uGroupID == 1 || uGroupID == 2)
					strResult = @"success";
				else
					strResult = @"UserName or Password is error!";
			}
			else{
				strResult = @"failed!";
			}
			Response.Clear();

			HttpCookie dortc = new HttpCookie(@"DoResult", strResult);
			Response.Cookies.Set(dortc);
		}
		else if(nActID == 2){
			int		bcFlag = 0;
			int.TryParse(Request.QueryString["nonerandom"], out bcFlag);

			string strSrcToken = (string)Session["uploadflag"];
			if (strSrcToken == null || strSrcToken.Length != 8)
				return;
			HttpCookie hc = Request.Cookies.Get(@"uploadflag");
			string	str = hc.Value;
			str = HttpUtility.UrlDecode(str, Encoding.UTF8);
			string strSrc = OperateControl.StringDecrypt(str);
			if(strSrc != strSrcToken)
				return;

			byte[] input = Request.BinaryRead(Request.TotalBytes);
			int nLen = 0, i, nNum = 0;
			Response.Clear();
			String	strResult = "";
			if (input.Length > 0) nNum = input[0];
			if (input.Length > nNum + 1){
				for (i = 0; i < nNum; i++)
					nLen += input[i + 1];
			}
			if (input.Length == nNum + 1 + nLen)
			{
				SqlCommand		cmd	= null;
				SqlConnection	conn = null;
				try{
					cmd	= new SqlCommand();
					conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);
					MsgAccessor msgAcce = new MsgAccessor();

					int nPos = nNum + 1;
					for (i = 1; i <= nNum; i++)
					{
						Byte[] pData = new Byte[input[i]];
						Array.Copy(input, nPos, pData, 0, input[i]);
						String strData = Encoding.UTF8.GetString(pData);
						strData = OperateControl.StringDecrypt(strData);

						char[] pSplit = new char[1];
						pSplit[0] = '#';
						string[] strList = strData.Split(pSplit);
						if (strList.Length == 5){
							string strMD5PWD = OperateControl.GetMD5String(strList[1]);
							string strKind = "";
							if(strList[0].Length > 2){
								strKind += strList[0][0];
								strKind += strList[0][1];
							}
							strKind += strList[2];
							int nRet = msgAcce.AddBarCode(strList[0], strMD5PWD, strKind, strList[3], strList[4], bcFlag, conn, cmd);
							if (nRet != 20){
								throw new Exception(nRet.ToString());
							}
						}
						nPos += input[i];
					}
					strResult = @"success";
				}
				catch (System.Exception /*e*/){
					strResult = @"Please try again later!";
				}
				finally {
					if (cmd != null){
						cmd.Parameters.Clear();
						cmd.Dispose();
					}
					if(conn != null){
						conn.Close();
					}
				}
			}
			else
			{
				strResult = @"failed!";
			}
			HttpCookie dortc = new HttpCookie(@"DoResult", strResult);
			Response.Cookies.Set(dortc);
		}
		String strToken = OperateControl.AchieveCharaters(8);
		HttpCookie htc = new HttpCookie(@"uploadflag", strToken);
		Session["uploadflag"] = strToken;
		Response.Cookies.Set(htc);
	}
}
